package com.example.vehiclemanagesystem.service.Impl;

import com.example.vehiclemanagesystem.dao.MomentsMapper;
import com.example.vehiclemanagesystem.entity.Moments;
import com.example.vehiclemanagesystem.entity.MomentsExample;
import com.example.vehiclemanagesystem.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MomentsServiceImpl implements MomentsService {

    @Autowired
    private MomentsMapper momentsMapper;

    /**
     * 查询所有动态信息
     * @param userId  当前登录用户唯一标识
     * @return  所有动态信息，包含评论和点赞信息
     */
    @Override
    public List<Moments> getMomentsAll(Long userId) {

        MomentsExample example = new MomentsExample();
        //根据创建时间倒序排列，最新的动态信息在最上面显示
        example.setOrderByClause("create_time desc");
        //查询全部动态信息
        List<Moments> moments = momentsMapper.selectByExample(example);
        for (Moments moment:moments){
            moment.setStrCreateTime(DateUtil.dateToString(moment.getCreateTime(),"yyyy-MM-dd HH:mm"));
        }
        //返回所有动态信息
        return moments;
    }

    @Override
    public int save(Moments moments) {
        Date date = new Date();
        moments.setCreateTime(date);
        moments.setUpdateTime(date);
        return momentsMapper.insert(moments);
    }

    @Override
    public int delete(Long id) {
        return momentsMapper.deleteByPrimaryKey(id);
    }


}
